package com.gcxy.jk.shixunyi.service;

import com.gcxy.jk.shixunyi.dao.OrderMapper;
import com.gcxy.jk.shixunyi.dao.UserMapper;
import com.gcxy.jk.shixunyi.entity.Order;
import com.gcxy.jk.shixunyi.entity.User;
import com.gcxy.jk.shixunyi.enums.OrderStatusEnum;
import com.gcxy.jk.shixunyi.utils.GetNowTime;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Author xujianlun
 * @Date 2022/10/1 20:15
 * @注释 订单信息
 */
@Service
@Slf4j
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增订单页面
     * @param openid
     * @param amount
     * @param togetherNumber
     * @param reservationTime
     * @param status
     * @param
     * @return
     */
    public String createOrder(Long amount, Integer togetherNumber
            , LocalDateTime reservationTime, OrderStatusEnum status,String openid){

        //订单页面增加
        String pass = "预购成功";
        String error = "错误！请先登录";

       //如果openid为空
       if (StringUtils.isBlank(openid)){
           return error;
        }else {
           //先去查询用户的信息
           Example example = Example.builder(User.class).build();
           example.createCriteria().andLike("openid", openid);
           User user = userMapper.selectOneByExample(example);
           Long uid = user.getId();
           Order order = new Order();
           order.setAmount(amount);
           order.setTogetherNumber(togetherNumber);
           order.setReservationTime(reservationTime);
           order.setStatus(status);

           //设置当前的时间存入数据库
           order.setCreateTime(GetNowTime.getNowTime());
           order.setUpdateTime(GetNowTime.getNowTime());
           //绑定用户id
           order.setCreateUser(uid);
           order.setUpdateUser(uid);
           orderMapper.insert(order);
           //再把订单信息绑定到用户订单
       }

        return pass;
    }

    /**
     * 根据用户id获取订单信息
     * @param id
     * @return
     */
    public  List<Order> getOrderByUserId(Long id) {

        Long createUser = id;

        Example example = Example.builder(User.class).build();
        example.createCriteria().andLike("createUser", String.valueOf(createUser));
        List<Order> orderList = orderMapper.selectByExample(example);
        return orderList;
    }

    /**
     * 所有订单
     * @return
     */
    public  List<Order> getAllOrder() {
        List<Order> orderList = orderMapper.selectAll();
        return orderList;
    }
}
